package com.hmall.order.task;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hmall.order.pojo.Order;
import com.hmall.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    IOrderService orderService;

    @Scheduled(cron = "0 * * * * ?")
    public void task() {
        LocalDateTime time = LocalDateTimeUtil.now().plusMinutes(-30);
        List<Order> list = orderService.lambdaQuery().le(Order::getCreateTime, time).eq(Order::getStatus, 1).list();
        if (ObjectUtil.isEmpty(list)) {
            log.info("没有未支付的订单");
        } else {
            List<Order> collect = list.stream().peek(l -> l.setStatus(5)).collect(Collectors.toList());
            orderService.updateBatchById(collect);
            log.info("未支付的订单已处理");
        }
    }
}
